package com.test.prolenders.view_prolenders.process_widget_prolenders;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.fragment.app.FragmentActivity;

import com.test.prolenders.R;
import com.test.prolenders.base_prolenders.BaseProcessWidgetManager_prolenders;
import com.test.prolenders.bean_prolenders.config_prolenders.AppShowCustomize_prolenders;
import com.test.prolenders.bean_prolenders.process_prolenders.ProcessInfo_Data_prolenders;
import com.test.prolenders.bean_prolenders.userinfo_prolenders.AccountList_Fr_prolenders;
import com.test.prolenders.bean_prolenders.userinfo_prolenders.BankAccount_Fr_prolenders;
import com.test.prolenders.bean_prolenders.userinfo_prolenders.OthersInfo_prolenders;
import com.test.prolenders.dialog_prolenders.BindAccountChannelDialog_Fr_prolenders;
import com.test.prolenders.dialog_prolenders.BindAccountDialog_Fr_prolenders;
import com.test.prolenders.dialog_prolenders.DeleteAccountDialog_Fr_prolenders;
import com.test.prolenders.util_prolenders.http_prolenders.HttpUtil_prolenders;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;

public class BindAccountViewManager_Fr_prolenders extends BaseProcessWidgetManager_prolenders {

    private final FragmentActivity activity;

    private LinearLayout groupLayout;
    private TextView tipsTitleTv,tipsDescTv;

    private ArrayList<BankAccount_Fr_prolenders> accounts = new ArrayList<>();

    private final View.OnClickListener addAccountCl;

    private String bindChannelMobile = "";

    public BindAccountViewManager_Fr_prolenders(View view, ProcessInfo_Data_prolenders data, String pageTag){
        super(view,data,pageTag);
        activity = (FragmentActivity) view.getContext();
        addAccountCl = v -> {
            HttpUtil_prolenders.getInstance().saveBuriedPoint(page,"ADD_CLICK","");
            new BindAccountDialog_Fr_prolenders(activity, page,
                    mobile -> {
                        bindChannelMobile = mobile;
                        HttpUtil_prolenders.getInstance().requestAccountList_Fr(activity,this::refreshAccountList);
                    }
            ).show();
        };
     }

    @Override
    protected void init(){
        groupLayout = mView.findViewById(R.id.vpbf_layout_group);
        tipsTitleTv = mView.findViewById(R.id.vpbf_tv_tips_title);
        tipsDescTv = mView.findViewById(R.id.vpbf_tv_tips_desc);

        mView.findViewById(R.id.vpbf_view_add).setOnClickListener(v -> {
            addAccountCl.onClick(v);
        });

        String tipsTitle = AppShowCustomize_prolenders.getInstance().getBindAccountTipsTitle();
        String tipsDesc = AppShowCustomize_prolenders.getInstance().getBindAccountTipsDesc();

        if (!TextUtils.isEmpty(tipsTitle) && !TextUtils.isEmpty(tipsDesc)){
            tipsTitleTv.setText(tipsTitle);
            tipsDescTv.setText(tipsDesc);
        }

    }

    @Override
    public boolean checkInput() {
        return !accounts.isEmpty();
    }

    @Override
    public String getInput() {
        return String.valueOf(accounts.size());
    }

    //更新列表
    public void refreshAccountList(){
        groupLayout.removeAllViews();

        accounts = AccountList_Fr_prolenders.getInstance().getBankAccountList();

        int num = 1;
        int i=accounts.size()-1;
        while (i>accounts.size()-3 || i>=0){

            if (i>=0) addAccount(accounts.get(i),num++);
            else {
                View view = View.inflate(mView.getContext(), R.layout.item_add_account_fr_prolenders, null);
                TextView titleView = view.findViewById(R.id.iaaf_tv_title);
                titleView.append(" "+(num++));
                view.findViewById(R.id.iaaf_view_click).setOnClickListener(addAccountCl);
                groupLayout.addView(view);
            }

            i--;
        }

        //更新列表后，判断是否需要绑定运营商
        if (!TextUtils.isEmpty(bindChannelMobile) ){
            if (OthersInfo_prolenders.getInstance().getAddBindFlag()){
                new BindAccountChannelDialog_Fr_prolenders(activity, bindChannelMobile,
                        null,
                        () -> HttpUtil_prolenders.getInstance().addAccount_Fr(
                                activity,
                                bindChannelMobile,
                                bindChannelMobile,
                                OthersInfo_prolenders.getInstance().getChannel(),
                                () -> HttpUtil_prolenders.getInstance().requestAccountList_Fr(activity,this::refreshAccountList),
                                false
                        )

                ).show();
            }
        }

    }

    //添加卡
    private void addAccount(BankAccount_Fr_prolenders account, int num){

        if (account == null) return;

        String desc = account.getWalletDesc();

        View accountView = View.inflate(mView.getContext(), R.layout.item_account_fr_prolenders, null);
        TextView titleTv = accountView.findViewById(R.id.iaf_tv_title);
        ImageView logoIv = accountView.findViewById(R.id.iaf_iv_logo);
        TextView channelTv = accountView.findViewById(R.id.iaf_tv_channel);
        TextView mobileTv = accountView.findViewById(R.id.iaf_tv_mobile);
        ImageButton deleteIb = accountView.findViewById(R.id.iaf_ib_delete);

        titleTv.append(" "+num);
        mobileTv.setText(account.getWalletMobile());
        channelTv.setText(account.getWalletDesc());

        //删除卡
        deleteIb.setOnClickListener(v -> {
            HttpUtil_prolenders.getInstance().saveBuriedPoint(page,"DELETE_CLICK",account.getWalletMobile());
            new DeleteAccountDialog_Fr_prolenders(activity,account, page,
                    () -> HttpUtil_prolenders.getInstance().requestAccountList_Fr(activity,this::refreshAccountList)).show();
        });

        groupLayout.addView(accountView);

        //获取图标
        if (AppShowCustomize_prolenders.getInstance().getChannelLogoByDesc(desc) == null){
            new Thread(() -> {
                try {
                    if (TextUtils.isEmpty(account.getWalletLogo())) return;
                    if (!AppShowCustomize_prolenders.getInstance().hasAccountChannel()) return;
                    URL url = new URL(account.getWalletLogo());
                    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                    connection.setConnectTimeout(5000);
                    connection.setRequestMethod("GET");
                    if (connection.getResponseCode()==200){
                        InputStream inputStream = connection.getInputStream();
                        Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
                        AppShowCustomize_prolenders.getInstance().setChannelLogoByDesc(desc,bitmap);
                        new Handler(Looper.getMainLooper()).post(() -> logoIv.setImageBitmap(bitmap));
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }).start();
        }else logoIv.setImageBitmap(AppShowCustomize_prolenders.getInstance().getChannelLogoByDesc(desc));

    }

}
